<script setup>
import { ref } from 'vue'
import {Download, Plus, Upload} from "@element-plus/icons-vue";
import useTable from "@/utils/useTable";
import { http } from "@/api";
import {useRouter} from "vue-router";
import {sentimentTemplateDownloadUrl} from "@/utils/common";
import {ElMessage} from "element-plus";
const router = useRouter()
/**
 * 搜索
 */
const hotList = ref([
  {
    name: '0-5000',
    id: 0,
  },
  {
    name: '5000-1w',
    id: 1,
  },
  {
    name: '>1w',
    id: 2,
  }
])

const dateRange = ref([])
const changeDate = (val) => {
  if (!val) {
    searchParams.value.startCreate = ''
    searchParams.value.endCreate = ''
    return
  }
  searchParams.value.startCreate = val[0] || ''
  searchParams.value.endCreate = val[1] || ''
}
/**
 * 表格
 * @type {Ref<UnwrapRef<*[]>>}
 */
const {
  searchParams,
  pageNo,
  pageSize,
  total,
  changePage,
  changeSize,
  dataSource,
  search,
  reset,
  openAdd,
  openView,
  openEdit,
  openDelete
} = useTable({
  requestFn: http.sentiment,
  routerPre: 'sentiment',
  params: {
    "endCreate": "",
    "heat": '',
    "startCreate": "",
    "title": ""
  },
})
const updateTableRow = (row) => {
  http.sentiment.updateIndex({
    id: row.id,
    serialNumber: row.serialNumber
  }).then(res => {
    ElMessage.success('排序成功')
    search()
  })
}
const rankSetting = () => {
  router.push({
    path: `/sentiment/rank`
  })
}
const downloadTemplate = () => {
  const link = document.createElement('a')
  link.style.display = 'none'
  link.href = sentimentTemplateDownloadUrl
  link.setAttribute('download', '舆情导入模板.xlsx')
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
}
const openUpload = (file) => {
  http.sentiment.import(file.file).then(res => {
    ElMessage.success('导入成功')
    search()
  })
}
</script>

<template>
  <PageMain title="舆情发布">
    <el-form label-suffix="：" inline>
      <el-form-item label="舆情标题">
        <el-input clearable v-model="searchParams.name"></el-input>
      </el-form-item>
      <el-form-item label="热度">
        <el-select v-model="searchParams.heat">
          <el-option v-for="item in hotList" :label="item.name" :value="item.id" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发布时间">
        <el-date-picker clearable
            v-model="dateRange"
            @change="changeDate"
            type="daterange"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="add-box f-r a-c f-e">
      <el-button type="primary"  @click="rankSetting">舆情排行榜设置</el-button>
<!--      <el-button type="primary" :icon="Download" @click="downloadTemplate">下载模板</el-button>-->
<!--      <el-upload class="upload-button" :show-file-list="false"  :http-request="openUpload">-->
<!--        <el-button type="primary" :icon="Upload" >导入</el-button>-->
<!--      </el-upload>-->
      <el-button type="primary" :icon="Plus" @click="openAdd">添加</el-button>
    </div>
    <div class="table-box">
      <el-table :data="dataSource">
        <el-table-column label="舆情标题" prop="title"></el-table-column>
        <el-table-column label="发布时间" width="180px" prop="releaseTime">
        </el-table-column>
        <el-table-column label="点赞数" width="100px" prop="likeNum" />
        <el-table-column label="浏览数" width="100px" prop="browseNum" />
        <el-table-column label="转发数" width="100px" prop="forwardNum" />
        <el-table-column label="作者" prop="author" />
        <el-table-column label="舆情来源" prop="source" />
        <el-table-column label="排序" width="120px">
          <template #default="scope">
            <div style="display: flex; align-items: center">
              <el-input placeholder="请输入排序" v-model="scope.row.serialNumber" type="number" @blur="updateTableRow(scope.row)"/>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="热度" prop="heat"></el-table-column>
        <el-table-column label="操作" fixed="right" width="80px">
          <template #default="scope">
            <el-button type="text" @click="openView(scope.row)">查看</el-button>
            <el-button type="text" @click="openEdit(scope.row)">编辑</el-button>
            <el-button type="text" @click="openDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[10, 50, 100, 200]"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="changeSize"
          @current-change="changePage"
      />

    </div>
  </PageMain>
</template>

<style scoped lang="scss">

</style>
